package com.bi.onlineEducation.web.exception;

import com.bi.onlineEducation.exception.BizException;
import com.bi.onlineEducation.exception.BusinessHandleException;
import com.bi.onlineEducation.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常处理类
 */
@ControllerAdvice(basePackages = {"com.bi.onlineEducation.web.controller"})
@Slf4j
public class ControllerExceptionHandler {

    /**
     * 处理自定义的业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResultCode bizExceptionHandler(BizException e) {
        log.error("业务异常！原因:{}", e.getErrorMsg());
        return ResultCode.error(e.getErrorMsg());
    }

    /**
     * 处理其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultCode exceptionHandler(Exception e) {
        log.error("未知异常！原因:{}", e);
        return ResultCode.error("处理失败");
    }

    /**
     * 处理其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessHandleException.class)
    @ResponseBody
    public ResultCode businessHandleExceptionHandler(BusinessHandleException e) {
        log.error("业务处理异常！原因:{}", e.getMsg());
        return ResultCode.error(e.getCode(), e.getMsg());
    }
}
